//
//  BaseViewController.m
//  CurvesFit
//
//  Created by liu on 2016/11/20.
//  Copyright © 2016年 Nci Technology Inc. All rights reserved.
//

#import "BaseViewController.h"
#import "AppDelegate.h"
#import "afMenu.h"
#import "StatusBarColorManager.h"

@interface BaseViewController ()<SUDropMenuDelegate>

@end

@implementation BaseViewController{
    NSArray *_dataArray;
}


-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[StatusBarColorManager sharedStatusBarColorManager] setWindowColor:[UIColor clearColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self uiConfig];
    [[StatusBarColorManager sharedStatusBarColorManager] setWindowColor:[UIColor blackColor]];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:33/255.f green:33/255.f blue:33/255.f alpha:1.00];
}

-(void)uiConfig {
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btnBack setImage:[UIImage imageNamed:@"btn-left-w.png"] forState:UIControlStateNormal];//@"btn_back.png"
    [btnBack addTarget:self action:@selector(onRtnMainPage:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem* btniBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem = btniBack;
    
    UIImage *img = [UIImage imageNamed:@"ic-accuro-n.png"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = CGRectMake(0, 0, 19, 19);
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    self.navigationItem.titleView = imgView;
    
    
    UIImage *rightImg = [UIImage imageNamed:@"ic-list.png"];
    
    UIButton *rBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rBtn.frame = CGRectMake(0, 0, 25, 25);
    [rBtn setImage:rightImg forState:UIControlStateNormal];
    rBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rBtn addTarget:self action:@selector(selectLeftAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rBtn];
    self.navigationItem.rightBarButtonItem = rightButton;
    /*UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
     self.navigationItem.backBarButtonItem = item;*/
    self.navigationItem.backBarButtonItem = nil;
    
}

-(void)selectLeftAction:(UIButton *)sender {
    AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LoginBodyInfo* bodyInfo = pApp.m_bodyInfo;
    Byte nGender = bodyInfo.nGender;
    
    NSString *icString = (nGender==1)?@"ic_male.png":@"ic_female";
    icString = @"profileicon";
    NSArray *icons = @[[UIImage imageNamed: icString], [UIImage imageNamed: @"ic_logout.png"]];
    NSArray *titles = @[NSLocalizedString(@"Profile", nil),NSLocalizedString(@"Logout", nil)];
    
    popupView = [[SUPopupMenu alloc] initWithTitles: titles icons: icons menuItemSize:CGSizeMake(110, 44)];
    __weak typeof(self) weakSelf = self;
    popupView.delegate = weakSelf;
    CGPoint p = sender.center;
    
    p.y += 30;
    
    [popupView presentWithAnchorPoint: p];
}

- (void)dropMenuDidTappedAtIndex: (NSInteger)index {
    if (index ==0) {
        [self personInfo:nil];
    } else {
        [self userLogout:nil];
    }
    [popupView dismiss];
}

- (void) onRtnMainPage:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) personInfo:(id)sender
{
//    AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIStoryboard* mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIBodyProfileViewCtrl* vcBodyProf = [mainStory instantiateViewControllerWithIdentifier:@"bodyProfViewCtrl"];
    [self.navigationController pushViewController:vcBodyProf animated:YES];
}

- (void) userLogout:(id)sender
{
    
    BOOL bIos8 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
    if(bIos8) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* logoutAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Logout", nil) style:UIAlertActionStyleDestructive
                                                             handler:^(UIAlertAction * action) {
                                                                 AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                                                                 pApp.m_nLastHrate = 0;
                                                                 pApp.m_nCurtAvgHrt = 0;
                                                                 
                                                                 [pApp userLogout];
                                                             }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
//                                                                 _lblMsg.text = NSLocalizedString(@"Cancel", nil);
                                                             }];
        
        [alert addAction:logoutAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Logout comfirm", nil) message:NSLocalizedString(@"Are you sure to logout!", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Logout", nil), nil];
        [alertView show];
    }
}

-(void)setBackgroundImageWithImageName:(NSString *)imageName {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imgView.image = [UIImage imageNamed:imageName];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view insertSubview:imgView atIndex:0];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end
